package com.sds.sweng.swat.miplatform.service.graphics;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import org.dom4j.Node;
import org.eclipse.draw2d.geometry.Rectangle;

public class ComponentUtils {

	public static Rectangle getComponentBound(Node component, Node parent) {
		if (parent == null) {
			return getComponentBound(component);
		}

		Rectangle bound = getComponentBound(component);

		while (!"Window".equals(parent.getName())) {
			Rectangle parentBound = getComponentBound(parent);

			bound.x += parentBound.x;
			bound.y += parentBound.y;

			parent = parent.selectSingleNode("..");
		}

		return bound;
	}

	public static Rectangle getComponentBound(Node component) {
		int x = Integer.parseInt(StringUtils.defaultIfEmpty(component.valueOf("@Left"), "0"));
		int y = Integer.parseInt(StringUtils.defaultIfEmpty(component.valueOf("@Top"), "0"));
		int width = Integer.parseInt(StringUtils.defaultIfEmpty(component.valueOf("@Width"), "0"));
		int height = Integer.parseInt(StringUtils.defaultIfEmpty(component.valueOf("@Height"), "0"));

		return new Rectangle(x, y, width, height);
	}

}
